package me.ivps.android.weibo.othercomponent;

import me.ivps.android.weibo.support.settinghelper.SettingUtility;
import me.ivps.android.weibo.support.utils.Utility;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * 网络连接监听器
 * 
 * User: Jiang Qi Date: 12-8-6
 */
public class ConnectionChangeReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent intent) {
        
        judgeNetworkStatus(context);
        
    }
    
    /**
     * 判断网络连接状态, 并启动是否自动获取消息任务
     * 
     * @param context
     */
    public static void judgeNetworkStatus(Context context) {
        if (Utility.isConnected(context)) {
            if (SettingUtility.getEnableFetchMSG()) {
                AppNewMsgAlarm.startAlarm(context, true);
            }
            else {
                AppNewMsgAlarm.stopAlarm(context, false);
            }
            
            decideTimeLineBigPic(context);
            decideCommentRepostAvatar(context);
        }
        else {
            AppNewMsgAlarm.stopAlarm(context, false);
        }
    }
    
    /**
     * 判断是否加载大图
     * 
     * @param context
     */
    private static void decideTimeLineBigPic(Context context) {
        if (SettingUtility.getListAvatarMode() == 3) {
            SettingUtility.setEnableBigAvatar(Utility.isWifi(context));
        }
        if (SettingUtility.getListPicMode() == 3) {
            SettingUtility.setEnableBigPic(Utility.isWifi(context));
        }
    }
    
    /**
     * 设置是否加载评论/转发头像
     * 
     * @param context
     */
    private static void decideCommentRepostAvatar(Context context) {
        if (SettingUtility.getCommentRepostAvatar() == 3) {
            SettingUtility
                    .setEnableCommentRepostAvatar(Utility.isWifi(context));
        }
    }
}